﻿<%@ Page Language="C#" MasterPageFile="~/Manager.master" AutoEventWireup="true" CodeFile="OrderApp_v1.aspx.cs"
    Inherits="Manager_OrderApp_OrderApp_v1" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

    <script language="javascript" src="../../Pub/Js/jquery.js" type="text/javascript"></script>

 <link href="<%= ResolveClientUrl("~/")%>Pub/Css/JQ/jquery.autocomplete.css" rel="stylesheet"
        type="text/css" />

    <script src="<%= ResolveClientUrl("~/")%>Pub/Js/JQ/jquery.autocomplete.js" type="text/javascript"></script>

    <script language="javascript" type="text/javascript">
                 var Url='<%= ResolveClientUrl("~/")%>';
               function fnDisplayTextMoneyPrice(obj)
                {
                    obj.value=FormatMoney(obj.value.ReplaceAll(",",""))   ;                    
                }  
              
              
              
              $(document).ready(function() 
             {   
                 
                     $str='<%=sData %>';   
                     data = eval("(" + $str + ')');
                     var $obj;    
                     BindAutoComplete($(".txtPDName"));
                     function BindAutoComplete($objInput)  
                     {
                           $objInput.autocomplete(data, {
                                minChars: 1,
	                            width: 400,		      
	                            selectFirst: true,
	                          //   autoFill: true,
                              formatItem: function(item) {                           
                                return item.PDName;
                              }
                            }).result(function(event, item) {  
                                if(item.PDID!="")      
                                { 
                                    $(this).next().val(item.PDID);                    
                                    $objDG=$(this).parent().parent().find("td").eq(4).find(".lblDonGia")    ;   
                                    $objDG.text(FormatMoney(item.Price.ReplaceAll(",","")));
                                    $(this).parent().parent().find("td").eq(4).find(".lblRate").text(item.Rate);
                                    $(this).css("background-color","LightGoldenrodYellow");
                                   
                                    $(this).parent().parent().find("td").eq(5).find(".txtSaleOfPrice").val("0");
                                    $(this).parent().parent().find("td").eq(5).find(".txtSaleOfPrice").removeAttr("onfocus");
                                    $(this).parent().parent().find("td").eq(5).find(".txtSaleOfPrice").css("background-color","white");
                                    
                                    if(item.KindNo=='F')
                                    {
                                        $(this).parent().parent().find("td").eq(3).find(".txtSoLuong").val("1");
                                        $(this).parent().parent().find("td").eq(3).find(".txtSoLuong").attr("onfocus","fnReadOnlyText('aaa')");
                                        $(this).parent().parent().find("td").eq(3).find(".txtSoLuong").css("background-color","#eaeaea");

                                    }
                                    else
                                    {
                                        $(this).parent().parent().find("td").eq(3).find(".txtSoLuong").val("1");
                                        $(this).parent().parent().find("td").eq(3).find(".txtSoLuong").removeAttr("onfocus");
                                        $(this).parent().parent().find("td").eq(3).find(".txtSoLuong").css("background-color","white");

                                    }
                                   
                                    
                                    
                                    fnChoosePDName($(this).parent().parent().find("td").eq(5).find(".txtSaleOfPrice")); 
                                 } 
                                else
                                 { 
                                      $(this).next().val("");  
                                  }
                            }).keyup(function(e){
                                  if(e.keyCode <9 ||  e.keyCode >45 ) 
                                  {
                                      $(this).css("background-color","white");
                                      $(this).next().val("");  //PDID
                                      
                                      $objDG=$(this).parent().parent().find("td").eq(4).find(".lblDonGia")    ;   
                                      $objDG.text("0");
                                      $(this).parent().parent().find("td").eq(4).find(".lblRate").text("VNĐ");                                      
                                      
                                      //Disable giam gia
                                      $(this).parent().parent().find("td").eq(5).find(".txtSaleOfPrice").val("0");
                                      $(this).parent().parent().find("td").eq(5).find(".txtSaleOfPrice").attr("onfocus","fnReadOnlyText('aaa')");
                                      $(this).parent().parent().find("td").eq(5).find(".txtSaleOfPrice").css("background-color","#eaeaea");
                                      
                                      //Reset lai so luong
                                      $(this).parent().parent().find("td").eq(3).find(".txtSoLuong").val("1");
                                      $(this).parent().parent().find("td").eq(3).find(".txtSoLuong").removeAttr("onfocus");
                                      $(this).parent().parent().find("td").eq(3).find(".txtSoLuong").css("background-color","white");
                                      
                                      fnChoosePDName($(this).parent().parent().find("td").eq(5).find(".txtSaleOfPrice")); 
                                       
                                       
                                   } 
                                    });      
                                        
                             
                         }     //end function
                        
                        
                          $(".imgNewRow").click(function(){         
                                       $('#<%=grvMaster.ClientID %> tr:last').after("<tr>"+$('#<%=grvMaster.ClientID %> tr:last').html()+"</tr>")
                                       $row=$('#<%=grvMaster.ClientID %> tr:last');                                       
                        
                                       $row.find("td").eq(1).text($('#<%=grvMaster.ClientID %> > tbody > tr').length-1);
                                       //====
                                       txtPDName=$row.find("td").eq(2).find(".txtPDName");
                                       $row.find("td").eq(2).find(".hidPDID").val("");
                                       txtPDName.attr("class","txtPDName");
                                       txtPDName.val("");
                                       txtPDName.css("background-color","white");
                                       BindAutoComplete(txtPDName);           
                                       //====So luong
                                       $row.find("td").eq(3).find(".txtSoLuong").val("1");       
                                       //====
                                       $row.find("td").eq(4).find(".lblDonGia").text("0");
                                       $row.find("td").eq(4).find(".lblRate").text("VNĐ");
                                       //====Giam gia
                                       $row.find("td").eq(5).find(".txtSaleOfPrice").val("0");                                             
                                       //====
                                       $row.find("td").eq(6).find(".lblTotal").val("0");      
                                       //====
                                       $row.find("td").eq(7).find(".txtNoteDetail").val("");             
                            });
                           
                           $("#btnSave").click(function()
                             {       
                                    var $UserID=$('#<%=hUserExc.ClientID %>').val();
                                    
                                    var $txtHoTen=$('#<%=txtHoTen.ClientID %>').val();
                                    var $txtDiaChi=$('#<%=txtDiaChi.ClientID %>').val();
                                    var $txtEmail=$('#<%=txtEmail.ClientID %>').val();
                                    var $ddlPlace=$('#<%=ddlPlace.ClientID %>').val();
                                    var $txtDienThoai=$('#<%=txtDienThoai.ClientID %>').val();
                                    var $txtTaxNo=$('#<%=txtTaxNo.ClientID %>').val();
                                     var $hidEmail=$('#<%=hidEmail.ClientID %>').val();
                                    
                                    
                                    $("#<%= hMethod.ClientID %>").val("");
                                    if($UserID=="" ) 
                                    {
                                     
                                        alert("Chọn khách hàng") ;
                                        return false;
                                    }
                                    if($txtHoTen=="") 
                                    {
                                     
                                        alert("Nhập vào tên khách hàng") ;
                                        return false;
                                     }
                                    if($txtDiaChi=="") 
                                    {
                                     
                                        alert("Nhập vào địa chỉ") ;
                                        return false;
                                     }
                                    if($ddlPlace=="") 
                                    {
                                     
                                        alert("Chọn tỉnh thành phố") ;
                                        return false;
                                     }
                                    if($hidEmail=="0") 
                                    {
                                     
                                        alert("Email này đã tồn tại") ;
                                        return false;
                                     }
                                    if($txtEmail!="") 
                                    {
                                     
                                        if(FcheckEmail("Email",$txtEmail)==false)                                
                                             return false;
                                     }
                                     
                                     if($txtDienThoai!="") 
                                    {
                                     
                                        if(FcheckNumber("Điện thoại",$txtDienThoai)==false)                                
                                             return false;
                                     }
                                     
                                    bErr="0"  ;
                                    $strXML="";
                                    $strXML+="<Method>SAVE</Method>";
                                    
                                    //For
                                   $('#<%=grvMaster.ClientID %> > tbody > tr').each(function() 
                                   {
                                        index=this.rowIndex;
                                        
                                       if (!index && index==0) return; 
                                        $row=$(this);
                                        PDNAME=$row.find("td").eq(2).find(".txtPDName").val();                                                                                   
                                        PDID=$row.find("td").eq(2).find(".hidPDID").val();
                                        QUANTITY=$row.find("td").eq(3).find(".txtSoLuong").val();
                                        PRICE=$row.find("td").eq(4).find(".lblDonGia").text();
                                        RATE=$row.find("td").eq(4).find(".lblRate").text();
                                        PRICESALEOFF=$row.find("td").eq(5).find(".txtSaleOfPrice").val();                                       
                                        TOTAL=$row.find("td").eq(6).find(".lblTotal").val();
                                        NOTEDETAIL=$row.find("td").eq(7).find(".txtNoteDetail").val();
                                        
                                        var sErr="STT: "+index+"-->";
                                         //check
                                         if(PDID=="")
                                         {
                                                bErr="1";
                                                alert(sErr+" chọn sản phẩm");
                                                return false;
                                         }  
                                         else if(QUANTITY<=0)
                                         {
                                                bErr="1";
                                                alert(sErr+" Nhập số lượng");
                                                return false;
                                         }                                         
                                        
                                         $strXML+="<OrderDetail>";
                                         $strXML+="<PDID>"+PDID+"</PDID>";
                                         $strXML+="<PDNAME>"+PDNAME+"</PDNAME>";
                                         $strXML+="<QUANTITY>"+QUANTITY+"</QUANTITY>";
                                         $strXML+="<PRICE>"+PRICE+"</PRICE>"; 
                                         $strXML+="<RATE>"+RATE+"</RATE>"; 
                                         $strXML+="<PRICESALEOFF>"+PRICESALEOFF+"</PRICESALEOFF>";
                                         $strXML+="<TOTAL>"+TOTAL+"</TOTAL>";                                         
                                         $strXML+="<NOTEDETAIL>"+NOTEDETAIL+"</NOTEDETAIL>";             
                                         $strXML+="</OrderDetail>";
                                    });                                       
                                 
                                                                      
                                    if(bErr=="1")  return false;
                                    
                                   
                      
                                     
                                    
                                   $("#<%= hMethod.ClientID %>").val($strXML);
                                   document.getElementById("aspnetForm").submit();                          
                        });   
                        
                        //Check email
                        function FcheckEmail(n,v){
                            var a=0
                            var p=0
                            for(var i=1;i<v.length;i++){
                            if(!v.charAt(i))return false
                            else if(v.charAt(i)=='@'){a++;if(v.charAt(i+1)==''){ alert(n+" không hợp lệ!");return false; }}
                            else if(v.charAt(i)=='.'){p++;if(v.charAt(i+1)==''||v.charAt(i+1)=='@'||v.charAt(i-1)=='@'){ alert(n+" không hop le!");return false; }}
                            }
                            if(a==1&&p){ return true; }
                            else { alert(n+" không hợp lệ!");return false; }
                            }

                            // kiem tra radio button da duoc chon chua
                            function FcheckRadio(n,v){
                            var r = false;
                            var i;
                            for (i = 0;  i < v.length;  i++){
                               if (v.checked)
                                   r = true;
                             }
                            if (!r){ alert("Chọn giá trị cho "+n+"!");return (false); }
                            else { return true; }
                        }    
                        //Check Num
                        function FcheckNumber(n,v){
                            if((isNaN(v))||(v=="")){ alert(n+" phải là số!");return false; }
                            else { return true; }
                        }   
                 
             }); //end document.ready
   
    </script>

    <div style="text-align: right; width: 100%">
        <asp:Label ID="lblDate" runat="server" Text=""></asp:Label></div>
    <div style="text-align:center">
        <table border="0" cellpadding="4" cellspacing="0" style="width: 99%">
            <tr>
                <td>
                    <div style="width: 100%; padding: 2px">
                        <table border="0" cellpadding="0" cellspacing="0" width="100%">
                            <tr>
                                <td valign="top">
                                    <table border="0" cellpadding="2" cellspacing="0" width="100%">
                                        <tr>
                                            <td style="height: 14px; width: 5%; white-space: nowrap;">
                                                <img alt="Tạo user mới" height="30" onclick="fnAddUser()" src="../../Images/Icon/add-user-icon.png"
                                                    style="cursor: pointer" title="Chọn User" width="30" /><img alt="chọn user đã tồn tại"
                                                        height="30" onclick="fnViewUser()" src="../../Images/Icon/user-icon.png" style="cursor: pointer"
                                                        title="Chọn User" width="30" />&nbsp;
                                            </td>
                                            <td align="left" style="height: 14px" valign="bottom">
                                                <asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Size="14px" Text="Thông tin của khách hàng"></asp:Label></td>
                                            <td align="right" style="height: 14px" valign="bottom">
                                                <asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" ShowMessageBox="True"
                                                    ShowSummary="False" ValidationGroup="grCustomer" />
                                                <input id="hidUSD" type="hidden" runat="server" /><asp:Label ID="lblUSD" runat="server"></asp:Label></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <table border="0" cellpadding="2" cellspacing="0" style="width: 100%">
                                        <tr>
                                            <td align="right" class="td_GuestInfo" nowrap="nowrap" style="width: 2%; white-space: nowrap;">
                                                Họ tên:</td>
                                            <td align="left" nowrap="nowrap" style="width: 40%; white-space: nowrap">
                                                <asp:TextBox ID="txtHoTen" runat="server" BackColor="#EAEAEA" CssClass="txt_Product"
                                                    ReadOnly="True" ValidationGroup="grCustomer" Width="80%"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtHoTen"
                                                    ErrorMessage="Nhập vào tên khách hàng" ForeColor="Red" ValidationGroup="grCustomer">*</asp:RequiredFieldValidator></td>
                                            <td align="right" nowrap="nowrap" style="width: 2%; white-space: nowrap">
                                                Tỉnh, Thành phố:</td>
                                            <td align="left" nowrap="nowrap" style="width: 30%; white-space: nowrap">
                                                <asp:DropDownList ID="ddlPlace" runat="server" Enabled="False">
                                                </asp:DropDownList></td>
                                        </tr>
                                        <tr style="color: #000000">
                                            <td align="right" class="td_GuestInfo" nowrap="nowrap" style="width: 2%; white-space: nowrap;">
                                                Địa chỉ:</td>
                                            <td align="left" nowrap="nowrap" style="width: 40%; white-space: nowrap">
                                                <asp:TextBox ID="txtDiaChi" runat="server" BackColor="#EAEAEA" CssClass="txt_Product"
                                                    ReadOnly="True" ValidationGroup="grCustomer" Width="80%"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtDiaChi"
                                                    ErrorMessage="Nhập vào địa chỉ" ForeColor="Red" ValidationGroup="grCustomer">*</asp:RequiredFieldValidator></td>
                                            <td align="right" nowrap="nowrap" style="width: 2%; white-space: nowrap">
                                                Điện thoại:</td>
                                            <td align="left" nowrap="nowrap" style="width: 30%; white-space: nowrap">
                                                <asp:TextBox ID="txtDienThoai" runat="server" BackColor="#EAEAEA" CssClass="txt_Product"
                                                    ReadOnly="True" ValidationGroup="grCustomer" Width="80%"></asp:TextBox><asp:RequiredFieldValidator
                                                        ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtDienThoai"
                                                        ErrorMessage="Nhập vào điện thoại" ForeColor="Red" ValidationGroup="grCustomer">*</asp:RequiredFieldValidator></td>
                                        </tr>
                                        <tr>
                                            <td align="right" class="td_GuestInfo" nowrap="nowrap" style="width: 2%; white-space: nowrap;">
                                                Email:</td>
                                            <td align="left" nowrap="nowrap" style="width: 40%; white-space: nowrap">
                                                <asp:TextBox ID="txtEmail" runat="server" BackColor="#EAEAEA" CssClass="txt_Product"
                                                    onblur="fnCheckEmail(this.value)" onkeyup="fnHideImgEmail()" ReadOnly="True"
                                                    ValidationGroup="grCustomer" Width="60%"></asp:TextBox>&nbsp;
                                                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail"
                                                    ErrorMessage="Yêu cầu nhập chính xác địa chỉ Email" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                                                    ValidationGroup="grCustomer">*</asp:RegularExpressionValidator>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator51" runat="server" ControlToValidate="hidEmail"
                                                    CssClass="valid_common" ErrorMessage="Email này đã được sử dụng" InitialValue="0"
                                                    ValidationGroup="grCustomer"> &nbsp;</asp:RequiredFieldValidator>
                                                <img id="imgStateEmail" border="0" src="../../Images/Icon/spinner.gif" style="visibility: hidden" />
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator52" runat="server" ControlToValidate="hidEmail"
                                                    CssClass="valid_common" ErrorMessage="Đang xử lý..." ValidationGroup="grCustomer">Đang xử lý...</asp:RequiredFieldValidator>
                                                <asp:TextBox ID="hidEmail" runat="server" CssClass="cssHidden" Text="1" Width="5px"></asp:TextBox>
                                            </td>
                                            <td align="right" nowrap="nowrap" style="width: 2%; white-space: nowrap">
                                                <asp:Label ID="Label5" runat="server" Text="Mã số thuế:"></asp:Label></td>
                                            <td align="left" nowrap="nowrap" style="width: 30%; white-space: nowrap">
                                                <asp:TextBox ID="txtTaxNo" runat="server" BackColor="#EAEAEA" CssClass="txt_Product"
                                                    ReadOnly="True" ValidationGroup="grCustomer" Width="80%"></asp:TextBox></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </div>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:GridView ID="grvMaster" runat="server" AutoGenerateColumns="False" Width="100%"
                        CellPadding="1" BorderWidth="1px" BorderColor="#407db3">
                        <Columns>
                            <asp:TemplateField HeaderText="X&#243;a">
                                <ItemTemplate>
                                    <img src='../../Images/Icon/cmdDelete.gif' border='0' alt='Delete' onclick="fnDeleteRow($(this))"
                                        style="cursor: pointer">
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" HorizontalAlign="Center" />
                                <HeaderStyle CssClass="cssBorderGrv" HorizontalAlign="Center" VerticalAlign="Middle"
                                    Wrap="False" Width="1%" />
                                <FooterStyle CssClass="cssBorderGrv" />
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    <img src="../../Images/Icon/add_icon.png" class="imgNewRow" border="0" />
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <%#Container.DisplayIndex + 1%>
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" HorizontalAlign="Center" />
                                <HeaderStyle CssClass="cssBorderGrv" HorizontalAlign="Center" VerticalAlign="Middle"
                                    Wrap="False" Width="1%" />
                                <FooterStyle CssClass="cssBorderGrv" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="T&#234;n sản phẩm">
                                <ItemTemplate>
                                    <asp:TextBox ID="lblTenSP" runat="server" Text='' Width="98%" CssClass="txtPDName"></asp:TextBox>
                                    <input type="hidden" runat="server" id="hidPDID" class="hidPDID" />
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" HorizontalAlign="Left" />
                                <HeaderStyle CssClass="cssBorderGrv" HorizontalAlign="Left" VerticalAlign="Middle"
                                    Wrap="False" />
                                <FooterStyle CssClass="cssBorderGrv" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Số lượng">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtSoLuong" runat="server" Width="40px" CssClass="txtSoLuong" Height="17px"
                                        onkeyup="fnUpdateQuantity(this)" Style="vertical-align: middle; text-align: center"
                                        MaxLength="2" Text='1'></asp:TextBox>
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" HorizontalAlign="Center" Width="1%" />
                                <HeaderStyle CssClass="cssBorderGrv" HorizontalAlign="Center" VerticalAlign="Middle"
                                    Wrap="False" />
                                <FooterStyle CssClass="cssBorderGrv" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Giá">
                                <ItemTemplate>
                                    <asp:Label ID="lblDonGia" runat="server" Text='0' CssClass="lblDonGia"></asp:Label>
                                    <asp:Label ID="lblRate" runat="server" Text='VNĐ' CssClass="lblRate"></asp:Label>
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" HorizontalAlign="Center" Wrap="false" />
                                <HeaderStyle CssClass="cssBorderGrv" HorizontalAlign="Center" VerticalAlign="Middle"
                                    Wrap="False" Width="10%" />
                                <FooterStyle CssClass="cssBorderGrv" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText='Giá mới'>
                                <ItemStyle HorizontalAlign="Center" Width="2%" />
                                <ItemTemplate>
                                    <asp:TextBox ID="txtSaleOfPrice" runat="server" Width="100%" CssClass="txtSaleOfPrice"
                                        onfocus="fnReadOnlyText(this)" Height="18px" Text='0' Style="text-align: right;
                                        width: 120px; text-align: right; background-color: #eaeaea; width: 135px" onkeyup="fnCalSaleOf(this)"></asp:TextBox>
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" BorderWidth="1px" HorizontalAlign="Center" BorderStyle="Solid"
                                    BorderColor="#407db3" Wrap="false" Width="10%" />
                                <HeaderStyle CssClass="cssBorderGrv" BorderWidth="1px" BorderStyle="Solid" BorderColor="#407db3" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Thanh toán (VNĐ)">
                                <ItemStyle HorizontalAlign="Center" Width="2%" />
                                <ItemTemplate>
                                    <asp:TextBox ID="lblTotal" runat="server" Text='0' Width="100%" onfocus="fnReadOnlyText(this)"
                                        CssClass="lblTotal" Style="text-align: right; border: 0; background-color: Transparent;
                                        width: 135px"></asp:TextBox>
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" BorderWidth="1px" HorizontalAlign="Center" BorderStyle="Solid"
                                    BorderColor="#407db3" Wrap="false" />
                                <HeaderStyle CssClass="cssBorderGrv" BorderWidth="1px" BorderStyle="Solid" BorderColor="#407db3"
                                    Wrap="false" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Ghi chú">
                                <ItemStyle HorizontalAlign="Center" Width="2%" />
                                <ItemTemplate>
                                    <asp:TextBox ID="txtNoteDetail" runat="server" Width="150px" Font-Size="11px" Text=''
                                        Height="18px" CssClass="txtNoteDetail"></asp:TextBox>
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" BorderWidth="1px" HorizontalAlign="Center" BorderStyle="Solid"
                                    BorderColor="#407db3" Wrap="false" />
                                <HeaderStyle CssClass="cssBorderGrv" Wrap="false" BorderWidth="1px" BorderStyle="Solid"
                                    BorderColor="#407db3" />
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <div align="center">
                                <asp:Label ID="Label1" runat="server" Text="Chưa có sản phẩm nào trong giỏ hàng của bạn"
                                    CssClass="lbl_TextDefault"></asp:Label>
                            </div>
                        </EmptyDataTemplate>
                        <RowStyle CssClass="gvRowStyle" />
                        <AlternatingRowStyle CssClass="gvAlternatingRowStyle" />
                        <HeaderStyle CssClass="gvHeader" VerticalAlign="Middle" Wrap="False" />
                        <FooterStyle CssClass="gv_FooterBasket" BackColor="paleturquoise" />
                    </asp:GridView>
                </td>
            </tr>
            <tr>
                <td align="center">
                    <table border="1" cellpadding="2" cellspacing="0" style="width: 100%; border-collapse: collapse"
                        bordercolor="red">
                        <tr>
                            <td style="width: 25%; white-space: nowrap; background-color: #f2ffe4" align="center">
                                <asp:CheckBox ID="chkPayment" runat="server" Style="cursor: pointer" Text="Đã thanh toán đầy đủ"
                                    Font-Bold="True" /><table id="tblPaymoney" runat="server" cellpadding="3" cellspacing="0">
                                        <tr>
                                            <td style="white-space: nowrap; font-weight: bold">
                                                Số tiền trả:
                                            </td>
                                            <td>
                                                <asp:TextBox ID="txtPaidMoney" runat="server" CssClass="txt_Product" onkeyup="fnDisplayTextMoney(this)"
                                                    Style="text-align: right" Text="0" Width="120px"></asp:TextBox>
                                            </td>
                                            <td>
                                                <asp:TextBox ID="TextBox2" runat="server" BackColor="TRansparent" BorderStyle="None"
                                                    CssClass="txt_Product" Font-Size="11px" ReadOnly="true" Style="text-align: right"
                                                    Text="VNĐ" Width="28px"></asp:TextBox>
                                            </td>
                                        </tr>
                                    </table>
                            </td>
                            <td align="center" style="background-color: honeydew">
                                <table border="0" bordercolor="#407db3" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
                                    <tr>
                                        <td align="right" style="padding-right: 10px; white-space: nowrap" valign="bottom">
                                            <asp:Label ID="Label14" runat="server" Font-Bold="True" Text="Tổng số lượng: "></asp:Label></td>
                                        <td align="right" colspan="2" style="width: 150px; border-bottom: black thin dotted;
                                            white-space: nowrap" valign="bottom">
                                            <asp:TextBox ID="lblQuantity" runat="server" CssClass="txt_Product" Height="18px"
                                                onfocus="fnReadOnlyText(this)" Style="border-right: 0px; border-top: 0px; border-left: 0px;
                                                border-bottom: 0px; text-align: right" Width="150px" BackColor="Transparent">0</asp:TextBox>
                                            <td align="right" style="width: 2%; white-space: nowrap">
                                            </td>
                                            <td id="td1" runat="server" align="right" style="padding-right: 10px; white-space: nowrap"
                                                valign="bottom">
                                                <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Số tiền đã trả:"></asp:Label></td>
                                            <td id="td2" runat="server" align="right" style="border-bottom: black thin dotted;
                                                white-space: nowrap" valign="bottom">
                                                <asp:TextBox ID="txtRepaid" runat="server" CssClass="txt_Product" Height="18px" onfocus="fnReadOnlyText(this)"
                                                    Style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px;
                                                    text-align: right" Width="150px" BackColor="Transparent">0</asp:TextBox>
                                                <asp:TextBox ID="txt" runat="server" BorderStyle="None" CssClass="txt_Product" Font-Size="11px"
                                                    ReadOnly="true" Style="text-align: right" Text="VNĐ" Width="28px" BackColor="Transparent"></asp:TextBox></td>
                                    </tr>
                                    <tr>
                                        <td align="right" style="padding-right: 10px; white-space: nowrap" valign="bottom">
                                            <asp:Label ID="Label15" runat="server" Font-Bold="True" Text="Tổng số tiền thanh toán: "></asp:Label></td>
                                        <td align="right" colspan="2" style="border-bottom: black thin dotted; white-space: nowrap"
                                            valign="bottom">
                                            <asp:TextBox ID="lblTongsotien" runat="server" CssClass="txt_Product" Height="18px"
                                                onfocus="fnReadOnlyText(this)" Style="border-right: 0px; border-top: 0px; border-left: 0px;
                                                border-bottom: 0px; text-align: right" Width="150px" BackColor="Transparent">0</asp:TextBox>
                                            <asp:TextBox ID="TextBox4" runat="server" BorderStyle="None" CssClass="txt_Product"
                                                Font-Size="11px" ReadOnly="true" Style="text-align: right" Text="VNĐ" Width="28px"
                                                BackColor="Transparent"></asp:TextBox></td>
                                        <td align="right" style="width: 2%; white-space: nowrap">
                                        </td>
                                        <td id="td3" runat="server" align="right" style="padding-right: 10px; white-space: nowrap"
                                            valign="bottom">
                                            <asp:Label ID="Label3" runat="server" Font-Bold="True" Text="Số tiền còn thiếu lại:"></asp:Label></td>
                                        <td id="td4" runat="server" align="right" style="border-bottom: black thin dotted;
                                            white-space: nowrap" valign="bottom">
                                            <asp:TextBox ID="txtRemainPrice" runat="server" CssClass="txt_Product" Height="18px"
                                                onfocus="fnReadOnlyText(this)" Style="border-right: 0px; border-top: 0px; border-left: 0px;
                                                border-bottom: 0px; text-align: right" Width="150px" BackColor="Transparent">0</asp:TextBox>
                                            <asp:TextBox ID="TextBox1" runat="server" BorderStyle="None" CssClass="txt_Product"
                                                Font-Size="11px" ReadOnly="true" Style="text-align: right" Text="VNĐ" Width="28px"
                                                BackColor="Transparent"></asp:TextBox>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td align="center" style="height: 32px">
                </td>
            </tr>
            <tr>
                <td align="center">
                    <table style="width: 100%" id="tdNote" cellspacing="0" cellpadding="0" border="0"
                        runat="server">
                        <tbody>
                            <tr>
                                <td style="padding-left: 10px; width: 10%" align="center">
                                    <asp:Label Style="padding-left: 5%" ID="lblNoteOrder" runat="server" Text="Ghi chú"></asp:Label></td>
                                <td>
                                    <asp:TextBox ID="txtNote" runat="server" CssClass="txt_Product" Width="95%" BorderStyle="Groove"
                                        Height="67px" TextMode="MultiLine"></asp:TextBox>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
            <tr>
                <td align="center">
                    <asp:Button ID="Button1" runat="server" CssClass="cssButton" OnClick="Button1_Click"
                        Text="Tạo đơn mới" />&nbsp;
                    <input id="btnSave" class="cssButton" type="button" value="Đặt hàng"  style="width:110px"/>
                    &nbsp;&nbsp;&nbsp;
                </td>
            </tr>
            <tr>
                <td align="center">
                    &nbsp;</td>
            </tr>
            <tr>
                <td align="center">
                </td>
            </tr>
        </table>
    </div>
    <input type="text" readonly="readonly" id="txtReadOnly" runat="server" style="text-align: right;
        border: 0" />
    <input type="hidden" runat="server" id="hidRemainPrice" />
    <input type="hidden" runat="server" id="hidRepaid" />
    <input type="hidden" runat="server" id="hidOrderDelete" />
    <input type="hidden" id="hMethod" runat="server" />
    <input type="hidden" id="hUserExc" runat="server" />
    <input id="hPrintPage" runat="server" type="hidden" value="N" />

    <script language="javascript" type="text/javascript">
   
     function IsPrintPage()
    {
        var hPrintPage=document.getElementById("<%= hPrintPage.ClientID %>");
        if(confirm('Bạn có muốn in hóa đơn không???'))
            hPrintPage.value="S";
        else
            hPrintPage.value="N";
    }
    
   function fnAddUser()
    {
        var width=450;
        var height=450;
        var left = parseInt((screen.availWidth/2) - (width/2));
		var top = parseInt((screen.availHeight/2) - (height/2));		
        var feature="width="+width+",height="+height+",menu=0,scrollbars=1,status=1,location=0,resizable=1,left=" + left + ",top=20,screenX=" + left + ",screenY=" + top;
        var CodeID='<%= Request.QueryString["CodeID"]%>';
        window.open("../User/AddCustomer.aspx?Type=Admin&CodeID=" + CodeID ,"" ,feature)
    }
    function fnViewUser()
    {
        var width=750;
        var height=800;
        var left = parseInt((screen.availWidth/2) - (width/2));
		var top = parseInt((screen.availHeight/2) - (height/2));		
        var feature="width="+width+",height="+height+",menu=0,scrollbars=1,status=1,location=0,resizable=1,left=" + left + ",top=20,screenX=" + left + ",screenY=" + top;
        var CodeID='<%= Request.QueryString["CodeID"]%>';
        window.open("../User/ChooseUser.aspx?Type=NewCus&CodeID=" + CodeID ,"" ,feature)
    }
    function fnChooseUser(UserID)
    {
        var hUserExc=document.getElementById("<%= hUserExc.ClientID %>");
        hUserExc.value=UserID;
        var hMethod=document.getElementById("<%= hMethod.ClientID %>")   ;
        hMethod.value="<Method>CHOOSEUSER</Method><UserID>"+UserID+"</UserID>";

        var theform = document.forms["aspnetForm"];
		theform.submit();
    }
    
     /*=================Email================*/
    String.prototype.trim = function () {
		return this.replace(/^\s*/, "").replace(/\s*$/, "");
	}
    function fnHideImgEmail()
    {
        var imgStateEmail=document.getElementById("imgStateEmail");
        imgStateEmail.style.visibility="hidden"; 
    }
    function fnCheckEmail(sValue)
    {  
        if(sValue.trim()=="")  return;
        
        var hUserExc=document.getElementById("<%= hUserExc.ClientID %>");
        Manager_OrderApp_OrderDetailApp.AjaxCheckEmail(sValue,hUserExc.value,CallBackEmail);  
     
    }
    function CallBackEmail(res)
    {
        if(res!=null)
        {
            var flag=res.value;
            if(flag!=null && flag!="")
			{	
			    var imgStateEmail=document.getElementById("imgStateEmail");

			    var hidEmail=document.getElementById("<%= hidEmail.ClientID%>");
			    hidEmail.value="aaa"; //Tranh su kien dang su ly
		
			    if(flag=="yes")
			    {   imgStateEmail.style.visibility="visible";
			        imgStateEmail.src="../../Images/Icon/cmdDelete.gif";
			        imgStateEmail.alt="Email này đã tồn tại";			        
			        hidEmail.value="0";
			       
			    }		
			    else
			    {
			           imgStateEmail.style.visibility="hidden";
			    }	    
			}	
        }
    } 
   
    
     function fnReadOnlyText(obj)
    {
        document.getElementById("<%= txtReadOnly.ClientID %>").select();
        document.getElementById("<%= txtReadOnly.ClientID %>").focus();   
        return false;
            
    }
   
    

    function fnUpdateQuantity(obj)
    {
         obj.value=FormatMoney(obj.value.ReplaceAll(",",""))
         if(obj.value=="" || obj.value=="0")
            obj.value="1";
          var objSaleOf=obj.parentNode.parentNode.cells[5].children[0];
          if(objSaleOf.value!="" || objSaleOf.value!="0" ) 
          {                 
                objSaleOf.value="0" ;
                fnCalSaleOf(objSaleOf);                
           }
        
    }
    
        function fnDeleteRow($obj)
        {        
             if($('#<%=grvMaster.ClientID %> > tbody > tr').length<=2)
            {
                alert("Could not remove more") ;
               return; 
            }  
               if(confirm('Delete??'))        
               {
                  $objTr=$obj.parent().parent();  
                  $("#<%=hidOrderDelete.ClientID %>").val($objTr.find("td").eq(1).find("span").html()+","+$("#<%=hidOrderDelete.ClientID %>").val())
                  $objTr.remove();                  
                   //Tinh lai tong so luong 
                   fnCalReTotal(); 
                   
                   //Uncheck Payment
                   $("#<%=chkPayment.ClientID %>").removeAttr('checked')  ;
                   $("#<%=txtRepaid.ClientID %>").attr({'value':'0'});
                  fnCalRepaid($("#<%=txtRepaid.ClientID %>"));   
              }
               
        }
        
        function fnChoosePDName(obj)
        {
            fnCalSaleOf(obj[0]);
          //alert( $objDG.html())
        }

       
        function fnCalSaleOf(obj)
        {            
               obj.value=FormatMoney(obj.value.ReplaceAll(",",""))   ;         
               var Quantity=obj.parentNode.parentNode.cells[3].children[0].value;
               var Rate=obj.parentNode.parentNode.cells[4].children[1].innerHTML;             
               var DiscountPrice=obj.value.ReplaceAll(",","");

              //neu = 0 lay lai gia goc 
               if(obj.value=="" || obj.value=="0")
               {   
                   DiscountPrice=obj.parentNode.parentNode.cells[4].children[0].innerHTML.ReplaceAll(",","");               
               }          
               var objTotalPrice=obj.parentNode.parentNode.cells[6].children[0];
               var TotalPrice= DiscountPrice*Quantity;           
               if (Rate=="USD")
               { 
                   var USD=document.getElementById("<%= hidUSD.ClientID %>").value ;
                   TotalPrice*= USD;                
                }
          
                objTotalPrice.value=FormatMoney(TotalPrice);    
              //Tinh lai tong so luong 
               fnCalReTotal(); 
               
               //Uncheck Payment
               $("#<%=chkPayment.ClientID %>").removeAttr('checked')  ;
               $("#<%=txtRepaid.ClientID %>").attr({'value':'0'});
                $("#<%=txtPaidMoney.ClientID %>").val("0");
              fnCalRepaid($("#<%=txtRepaid.ClientID %>"));        
        }      
        function fnCalReTotal()
        {          
                var sTotal=0,sQuantity=0; 
                $("#<%= grvMaster.ClientID %>").find('tr').each(function(index){
                    if(index!="0")
                    {                          
                        $objTR=$(this);                   
                        sTotal+=parseInt($objTR.find('td:eq(6)').find('input:eq(0)').attr('value').ReplaceAll(",","")); 
                        sQuantity+=parseInt($objTR.find('td:eq(3)').find('input:eq(0)').attr('value').ReplaceAll(",","")); 
                      
                    }
                });
                 sTotal=FormatMoney(sTotal);
                 
                $("#<%=lblQuantity.ClientID %>").val(sQuantity);
                $("#<%=txtRemainPrice.ClientID %>").attr({'value':sTotal});
                $("#<%=txtRepaid.ClientID %>").attr({'value':'0'}); 
                $("#<%=lblTongsotien.ClientID %>").attr({'value':sTotal})  ;          
           
        }
        
//*************************************************        
        //Tinh toan tra truoc
        $(document).ready(function() 
        {
            $("#<%=txtPaidMoney.ClientID %>").keyup(function()
            {
                  fnCalRepaid($(this));
            });
        });
        function fnDisplayTextMoney(obj)
        {
            obj.value=FormatMoney(obj.value.ReplaceAll(",",""))   ; 
        }
        function fnCalRepaid(obj)
        {
            if(obj.attr('value')!="")
            { 
                  $Remainpaid=parseInt(obj.attr('value').ReplaceAll(",",""));
            
                                  
                   //Tong tien-(so tien tra truoc+so tien tra truoc dang nhap)
                  $RemainPrice=$("#<%=lblTongsotien.ClientID %>").attr('value').ReplaceAll(",","")-$Remainpaid;
                   
                  if($RemainPrice<0)
                  {
                    alert('Bạn đã nhập vượt quá số tiền cần thanh toán');
                    obj.focus();
                    obj.attr({'value':'0'});
                    fnCalRepaid(obj);
                    return;
                  }
                  else if($RemainPrice==0 && $("#<%=lblTongsotien.ClientID %>").val()!="0")
                     $("#<%=chkPayment.ClientID %>").attr({'checked':'checked'})  ;
                  else
                     $("#<%=chkPayment.ClientID %>").removeAttr('checked')  ;
                     
                  $("#<%=txtRemainPrice.ClientID %>").attr({'value':FormatMoney($RemainPrice)}); 
                  $("#<%=txtRepaid.ClientID %>").attr({'value':FormatMoney($Remainpaid)});
                                     
            }
        }     
        
  //*************************************************
  $(document).ready(function() {
        $("#<%=chkPayment.ClientID %>").click(function()
        {           
                  fnCheckedPayment($(this));
        });
     }); 
   //*************************************************
   function fnCheckedPayment(obj)
   {               
               if(obj.attr('checked')==true)       //Da tra tien day du
              {
                  $("#<%=txtRemainPrice.ClientID %>").attr({'value':'0'});
                  $("#<%=txtRepaid.ClientID %>").attr({'value':$("#<%=lblTongsotien.ClientID %>").attr('value')})  ; 
                  $("#<%=tblPaymoney.ClientID%>").hide();
              }
              else
              {
                   
                   $("#<%=tblPaymoney.ClientID%>").show();                        
                     $("#<%=txtPaidMoney.ClientID %>").val("0");
                   
                  $("#<%=txtRepaid.ClientID %>").attr({'value':"0"})
                  $("#<%=txtRemainPrice.ClientID %>").attr({'value':$("#<%=lblTongsotien.ClientID %>").attr('value')})  ; 
              } 
   }
   
   //****************************************************
   
    String.prototype.trim = function () {
		return this.replace(/^\s*/, "").replace(/\s*$/, "");
	}
    function fnHideImgEmail()
    {
        var imgStateEmail=document.getElementById("imgStateEmail");
        imgStateEmail.style.visibility="hidden"; 
    }
    function fnCheckEmail(sValue)
    {  
        if(sValue.trim()=="")  return;
        
        var hUserExc=document.getElementById("<%= hUserExc.ClientID %>");
        Manager_OrderApp_OrderApp_v1.AjaxCheckEmail(sValue,hUserExc.value,CallBackEmail);  
     
    }
    function CallBackEmail(res)
    {
        if(res!=null)
        {
            var flag=res.value;
            if(flag!=null && flag!="")
			{	
			    var imgStateEmail=document.getElementById("imgStateEmail");

			    var hidEmail=document.getElementById("<%= hidEmail.ClientID%>");
			    hidEmail.value="aaa"; //Tranh su kien dang su ly
		
			    if(flag=="yes")
			    {   imgStateEmail.style.visibility="visible";
			        imgStateEmail.src="../../Images/Icon/cmdDelete.gif";
			        imgStateEmail.alt="Email này đã tồn tại";			        
			        hidEmail.value="0";
			       
			    }		
			    else
			    {
			           imgStateEmail.style.visibility="hidden";
			    }	    
			}	
        }
    }
    
        
     
        
    
    
    
    </script>

</asp:Content>


